package xl.util;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import xl.model.Document;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

// CSVLoader.java
public class CSVLoader {
    public List<Document> loadCSV(String filePath) throws IOException {
        List<Document> documents = new ArrayList<>();

        try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
            String[] nextLine;
            while ((nextLine = reader.readNext()) != null) {
                // 将CSV行转换为自然语言文本
                String content = String.join(" | ", nextLine);
                Document doc = new Document();
                doc.setId(UUID.randomUUID().toString());
                doc.setContent(content);
                documents.add(doc);
            }
        } catch (CsvValidationException e) {
            throw new RuntimeException(e);
        }
        return documents;
    }
}

